package com.spreedairy.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portlet.shopping.model.ShoppingItem;
import com.liferay.portlet.shopping.service.ShoppingItemLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.spreedairy.portal.model.Recommendation;
import com.spreedairy.portal.model.Wishlist;
import com.spreedairy.portal.model.impl.RecommendationImpl;
import com.spreedairy.portal.model.impl.WishlistImpl;
import com.spreedairy.portal.service.RecommendationLocalServiceUtil;
import com.spreedairy.portal.service.WishlistLocalServiceUtil;

public class CommonAction extends MVCPortlet{

	public static void addUserWish(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException {
		
		String action = ParamUtil.getString(actionRequest, "action");
		long itemId = Long.parseLong(ParamUtil.getString(actionRequest, "itemId"));
		
		ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
		long userId = themeDisplay.getUserId();
		long groupId = themeDisplay.getScopeGroupId();
		long companyId = themeDisplay.getCompanyId();
		if(Validator.isNotNull(action)){
			if(action.equalsIgnoreCase("wishList")){
				try {
						Wishlist wishList = new WishlistImpl();
						wishList.setItemId(itemId);
						wishList.setUserId(userId);
						wishList.setGroupId(groupId);
						wishList.setCompanyId(companyId);
						wishList.setCreateDate(new Date());
						WishlistLocalServiceUtil.addWishlist(wishList);
						actionRequest.setAttribute("action", "userWish");
						actionRequest.setAttribute("itemId", String.valueOf(itemId));
						actionResponse.setRenderParameter("jspPage", "/html/item/view_item.jsp");
				} catch (SystemException e) {
					e.printStackTrace();
				}
			}else if(action.equalsIgnoreCase("recommended")){
				try {
						Recommendation recommendation = new RecommendationImpl();
						recommendation.setItemId(itemId);
						recommendation.setUserId(userId);
						recommendation.setGroupId(groupId);
						recommendation.setCompanyId(companyId);
						recommendation.setCreateDate(new Date());
						RecommendationLocalServiceUtil.addRecommendation(recommendation);
						actionRequest.setAttribute("action", "userWish");
						actionRequest.setAttribute("itemId", String.valueOf(itemId));
						actionResponse.setRenderParameter("jspPage", "/html/item/view_item.jsp");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static List<ShoppingItem> getItemList(List<Long> itemIds){
		List<ShoppingItem> itemList = new ArrayList<ShoppingItem>();
		if(Validator.isNotNull(itemIds) && itemIds.size() > 0){
			for(long itemId: itemIds){
				ShoppingItem item = null;
				try {
					item = ShoppingItemLocalServiceUtil.getItem(itemId);
				} catch (PortalException e) {
					e.printStackTrace();
				} catch (SystemException e) {
					e.printStackTrace();
				}
				if(Validator.isNotNull(item)){
					itemList.add(item);
				}
			}
		}
		return itemList;
	}
	
}
